package com.tehui.utils.web;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

import java.util.ArrayList;

import static com.tehui.app.MyApplication.WX_APPID;

/**
 * Created by jasmine on 2017/6/15.
 */
//分享
public class ShareUtil {

    private Tencent     mTencent;
    private IWXAPI      api;
    private IUiListener listener;
    private int         type;
    private String      title;
    private String      summary;
    private String      url;
    private Bitmap      image;
    private String      ImageUrl;
    Activity context;

    public ShareUtil(Activity context, IUiListener listener) {
        mTencent = Tencent.createInstance("1106154936", context);
        api = WXAPIFactory.createWXAPI(context, WX_APPID, true);
        this.listener = listener;
        this.context = context;
    }

    //分享的类型;
    public ShareUtil setType(int type) {
        this.type = type;
        return this;
    }

    //标题
    public ShareUtil setTitle(String title) {
        this.title = title;
        return this;
    }

    //摘要
    public ShareUtil setSummary(String summary) {
        this.summary = summary;
        return this;
    }

    //url
    public ShareUtil setUrl(String url) {
        this.url = url;
        return this;
    }

    //图片对象
    public ShareUtil setImageBitmap(Bitmap image) {
        this.image = image;
        return this;
    }

    //图片链接
    public ShareUtil setImgUrl(String url) {
        this.ImageUrl = url;
        return this;
    }

    public void creat() {
        switch (type) {
            case 1:
                shareQQ();
                break;
            case 2:
                shareZone();
                break;
            case 3:
                shareWeChat();
                break;
            case 4:
                shareFriend();
                break;
        }
    }

    private void shareQQ() {
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        //标题
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        //摘要
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
        //链接
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        //图片链接
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, ImageUrl);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "21特惠");//app名称
        mTencent.shareToQQ(context, params, listener);
    }

    private void shareZone() {
        ArrayList<String> imageList = new ArrayList<>();
        imageList.add(ImageUrl);
        Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, summary);//选填
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageList);
        mTencent.shareToQzone(context, params, listener);
    }

    private void shareFriend() {
        WXWebpageObject wxWebpage = new WXWebpageObject();
        wxWebpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(wxWebpage);
        msg.title = title;
        msg.description = summary;
        msg.setThumbImage(image);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "transaction";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);

//        WXTextObject wxTextObject = new WXTextObject();
//        wxTextObject.text = summary;
//
//        WXMediaMessage msg = new WXMediaMessage(wxTextObject);
//        msg.mediaObject = wxTextObject;
//        msg.description = summary;
//
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.transaction = "transaction";
//        req.message = msg;
//        req.scene = SendMessageToWX.Req.WXSceneTimeline;
//        api.sendReq(req);

    }

    private void shareWeChat() {
        WXWebpageObject wxWebpage = new WXWebpageObject();
        wxWebpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(wxWebpage);
        msg.title = title;
        msg.description = summary;
        msg.setThumbImage(image);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "transaction";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);

//        WXTextObject wxTextObject = new WXTextObject();
//        wxTextObject.text = url;
//
//        WXMediaMessage msg = new WXMediaMessage(wxTextObject);
//        msg.mediaObject = wxTextObject;
//        msg.description = url;
//
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.transaction = "transaction";
//        req.message = msg;
//        req.scene = SendMessageToWX.Req.WXSceneSession;
//        api.sendReq(req);
    }

    //    /**
    //     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
    //     * @param bitmap
    //     * @return
    //     */
    //    public static byte[] bitmap2Bytes(Bitmap bitmap, int maxkb) {
    //        ByteArrayOutputStream output = new ByteArrayOutputStream();
    //        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    //        int options = 100;
    //        while (output.toByteArray().length > maxkb && options != 10) {
    //            output.reset(); //清空output
    //            bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
    // 这里压缩options%，把压缩后的数据存放到output中
    //            options -= 10;
    //        }
    //        bitmap.recycle();
    //        try {
    //            output.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //        return output.toByteArray();
    //    }

    //    private byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    //        ByteArrayOutputStream output = new ByteArrayOutputStream();
    //        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    //        if (needRecycle) {
    //            bmp.recycle();
    //        }
    //        byte[] result = output.toByteArray();
    //        try {
    //            output.close();
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    //        return result;
    //    }

    //    private String buildTransaction(final String type) {
    //        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System
    // .currentTimeMillis();
    //    }
}
